<?php

class Album_model extends CI_Model{
    
   var $album_path;
   var $album_path_url;
   var $file_name;
    
    //constructor
    function Album_model(){
        
        parent::__construct();
       
        $this->album_path = realpath (APPPATH . '../images'); // untuk langsung menunjuk ke url application folder dan diteruskan ke image folder
                                                    //realpath function untuk mendapatkan path sebenarnya: ngilangin "..."
        $this->album_path_url = base_url().'images/';
    }
    ///////////////////////////////////////////////////////////////////////////
    
    function do_upload(){
        
        //print_r($_FILES);
        if($_FILES['userfile']['name']==''){
            echo 'please choose file';
           return;
        }
        
        //if($_FILES)
        
        $config = array(
            
            
            'allowed_types' => 'jpg|jpeg|gif|png',
            'upload_path' => $this->album_path,
            'max_size'	=> '2000'
            
        );
        
        $this->load->library('upload');
        $this->upload->initialize($config);
        $this->upload->do_upload(); 
        $image_data = $this->upload->data();
        
       
       $this->file_name = $image_data['raw_name'] . $image_data['file_ext'];;
        
        //insert into database :setting array
        $data = array(
            'image_title' => $this->input->post('title'),
            'album_category'  => $this->input->post('category'),
            'album_file_name' => $this->file_name
        );
        
        //call the function to insert
        $this->insertDB($data);
        
        //print_r($image_data);
       
        
        //thumbnail images 
       $configThumb = array(
            
            'image_library' => 'gd2',
            'source_image' => $image_data['full_path'],
            'new_image'     => $image_data['file_path'] . 'thumbs/' . $image_data['raw_name'].$image_data['file_ext'], //clien_name : nama file yang akan dithumbkan
            'maintain_ratio' => TRUE,
            'width'          => '150',
            'height'         => '100',
           
            
        );
        
       
         $this->load->library('image_lib', $configThumb);
         if(!$this->image_lib->resize()){
             
           echo  $this->image_lib->display_errors();
         }
             
    }
    ///////////////////////////////////////////////////////////////////////////
    
    function get_images(){
        
      
        $query = $this->db->get('album');
        $files = array();
      
        foreach($query->result() as $row){
            
            $files[] = $row;
        }
        
        
        $images = array();
        
        foreach ($files as $file){
            
            $images[] = array(
                'album_id' => $file->album_id,
                'url' => $this->album_path_url . $file->album_file_name,
                'thumb_url' => $this->album_path_url . 'thumbs/' . $file->album_file_name,
                 'image_title'   => $file->image_title
                
            );            
        }
        
        return $images;
    }
    ///////////////////////////////////////////////////////////////////////////
    
    function insertDB($data){
        
        $this->db->insert('album', $data);
       
        if ($this->db->affected_rows() == '1') {
            return TRUE;
        }

        return FALSE;
    
        
    }
    ///////////////////////////////////////////////////////////////////////////
    
    function getAlbum_file_name($id){
        
        //get the album_file_name from db
      
             //$this->db->select('album_file_name');
             //$this->db->where('album_id', $id);
             //$this->db->from('album');
             $query = $this->db->get_where('album',array('album_id'=>$id));
             $file = null;
             foreach($query->result() as $row){
                 $file = $row;
             }
             
        return $file->album_file_name;
    }
    //////////////////////////////////////////////////////////////////////////
    function delete_image($id){
        
        //get the album_file_name
        $album_file_name = $this->getAlbum_file_name($id);
        
        //Delete from database 
        $this->db->where('album_id', $id);
        $this->db->delete('album'); 
        
       //lets delete the images from image and thumb folders
        unlink($this->album_path .'/'. $album_file_name);
       #lets delete the thumb.
       unlink($this->album_path . '/thumbs/'. $album_file_name);
                
    }
    //////////////////////////////////////////////////////////////////////////
}
?>
